//
//  SocketClientManager.m
//  Calibration
//
//  Created by ViKing Lai on 2018/1/24.
//  Copyright © 2018年 ViKing Lai. All rights reserved.
//

#import "SocketClientManager.h"
#import "SocketClient.h"
#define SOCKETTYPE @"Socket"


@interface SocketClientManager()
{
    SocketClient* client;
}

@end

@implementation SocketClientManager
{
    BOOL mStopped ;
}



-(instancetype)init
{
    if(self = [super init])
    {
        client = [[SocketClient alloc]init];
        
    }
    
    return self ;
}

- (void)writeCommand:(NSString *)testItem client:(SocketClient *)socketClient
{
    [socketClient getAllData] ;
    [socketClient sendData:testItem] ;

    [NSThread sleepForTimeInterval:0.01] ;
}

-(NSString*)readData:(NSString *)string client:(SocketClient*)socketClient endStr:(NSString*)endString
{
    NSString* strRtn = [[NSString alloc] init] ;
    
    if ([endString isEqualToString:@""]) {
        [NSThread sleepForTimeInterval:0.1] ;
        strRtn = [socketClient getAllData] ;

    }
    else{
        strRtn = [socketClient ReadTo:endString timeOut:5000 Interval:0.001] ;

    }
    
    
    return strRtn ;
}

-(NSData*)readDataByte:(NSString *)unit client:(SocketClient*)socketClient
{
    NSData * strRtn = [[NSData alloc] init] ;
    
    [NSThread sleepForTimeInterval:3000] ;
    strRtn = [socketClient getAllDataByte] ;
    
    return strRtn ;
}

-(NSString *)getInitData:(NSString *)command
{
    
    return command;
}



-(NSString*)getData:(NSString *)parameter endStr:(NSString*)string
{
    @autoreleasepool
    {
        NSString* strData= [[NSString alloc] init] ;
        
        @try
        {
            if(!client.isConnected)
            {
                if(![client connect])
                {
                    return nil ;
                }
                else
                {
                    [NSThread sleepForTimeInterval:0.01] ;
                }
            }
            else
            {
//                [NSThread sleepForTimeInterval:0.2] ;
            }
            
            
            [self writeCommand:parameter client:client] ;
            

            strData = [self readData:parameter client:client endStr:string] ;
            
            strData = [strData stringByReplacingOccurrencesOfString:@"\r\n" withString:@" "];
            strData = [strData stringByReplacingOccurrencesOfString:@"\r" withString:@" "];
            strData = [strData stringByReplacingOccurrencesOfString:@"\n" withString:@" "];
            strData = [strData stringByReplacingOccurrencesOfString:@" " withString:@""];
            
        } @catch (NSException *exception)
        {
        }
        
        return [self extractSerialData:strData param:parameter];
    }
}


-(NSData*)getDataByte:(NSString *)parameter
{
    @autoreleasepool
    {
        NSData* strData= [[NSData alloc] init] ;
        
        
        @try
        {
            if(!client.isConnected)
            {
                if(![client connect])
                {
                    return nil ;
                }
                else
                {
                    [NSThread sleepForTimeInterval:0.01] ;
                }
            }
            else
            {
                [NSThread sleepForTimeInterval:0.01] ;
            }
            
            
            [self writeCommand:parameter client:client] ;
            
            strData = [self readDataByte:parameter client:client] ;
            
            
            
        } @catch (NSException *exception)
        {
        }
        
        return strData;
    }
}

// 提取数据
- (NSString*) extractSerialData:(NSString *)data param:(NSString *)testItem
{
//
//    testItem.testReturnStr = data;
//    testItem.testItemVal =data;
//    // 数据为nil时，处理情况
//    if (data == nil || [data isEqual:@""]) {
//        testItem.testItemVal = @"(null)";
//        testItem.testReturnStr = @"Port disconnet or block or DUT problems";
//        return data;
//    }
//
//    Parser* parser = [[NSClassFromString(unit.parser) alloc] init];
//    unit = [parser parse:data usingTestUnit:unit];
    
//    if ([unit.testValue isEqual:@""] || ([[unit.testValue lowercaseString] rangeOfString:@"error"].length > 0)
//        || [[unit.testValue lowercaseString] rangeOfString:@"fail"].length > 0) {
//        unit.isPass = NO;
//    }
//    
//    if (unit.itemName != nil &&![unit.itemName isEqual:@""])
//        NSLog(@"[SERIAL]unit testResult is = %@, isPass = %hhd",unit.testValue, unit.isPass);
    
    
    
//    int iVal = arc4random() % 100;
//    testItem.testItemVal = [NSString stringWithFormat:@"%d",iVal];
    return data;
}



// For micky, it need send charactor one by one
-(NSString*)sendData:(NSString *)parameter
{
    @autoreleasepool
    {
        NSString* strData= [[NSString alloc] init] ;
        
        
        @try
        {
            if(!client.isConnected)
            {
                if(![client connect])
                {
                    return nil ;
                }
                else
                {
                    [NSThread sleepForTimeInterval:0.01] ;
                }
            }
            else
            {
                [NSThread sleepForTimeInterval:0.01] ;
            }
            [self writeCommand:parameter client:client] ;
        }
        @catch (NSException *exception)
        {
        }
        
        return [self extractSerialData:strData param:parameter];
    }
}

@end
